<?php
session_start();
error_reporting(E_ALL);

/**
 * reloj-checador - class.modelo.php
 *
 * $Id$
 *
 * This file is part of reloj-checador.
 *
 * Automatically generated on 22.08.2013, 19:51:48 with ArgoUML PHP module
 * (last revised $Date: 2010-01-12 20:14:42 +0100 (Tue, 12 Jan 2010) $)
 *
 * @author firstname and lastname of author, <author@example.org>
 */

if (0 > version_compare(PHP_VERSION, '5')) {
	die('This file was generated for PHP 5');
}

/* user defined includes */
// section -64--88-0-101-81690f7:140a816daa7:-8000:0000000000000968-includes begin
// section -64--88-0-101-81690f7:140a816daa7:-8000:0000000000000968-includes end

/* user defined constants */
// section -64--88-0-101-81690f7:140a816daa7:-8000:0000000000000968-constants begin
// section -64--88-0-101-81690f7:140a816daa7:-8000:0000000000000968-constants end

require_once 'views/view-login.php';
require_once 'class/class.conecction.php';
class modelo
{
	// --- ASSOCIATIONS ---


	// --- ATTRIBUTES ---

	// --- OPERATIONS ---

	public $user_name;
	public $user_pass;
	public $entry = 0;
	
	public $emp_name;
	public $emp_pass;
	public $text;
	public $id;
	public $cnn;

	public function modelo()
	{
		// section -64--88-0-101-81690f7:140a816daa7:-8000:000000000000096C begin
		$this->cnn = new conexion();
		// section -64--88-0-101-81690f7:140a816daa7:-8000:000000000000096C end
	}
	
	public function borrar_personal(){
		$query = "DELETE FROM personal WHERE id = '$this->id'";
		$result = $this->cnn->ejecutar($query);
		if ($result){
			header("location:?action=ver_personal");
		}
	}
	
	public function borrar_usuario(){
		$query = "DELETE FROM usuarios WHERE id = '$this->id'";
		$result = $this->cnn->ejecutar($query);
		if ($result){
			header("location:?action=ver_usuarios");
		}
	}
	
	public function ver_usuarios(){
		include_once 'views/view-usuarios.php';
		$usuario = new usuario();
	}
	
	public function view_add_usuario(){
		include_once 'views/view_new_usuario.php';
		$new_usuario = new new_usuario();
	}
	
	public function view_add_personal(){
		include_once 'views/view_new_personal.php';
		$new_personal = new new_personal();
	}
	
	public function get_id(){
		$result = false;
		
		WHILE($result == false){
			$array_nombre = str_split($_POST['tnombre_'],3);
			$array_apellido = str_split($_POST['tapellido'],3);
			$array_puesto = str_split($_POST['tpuesto'],2);
			$dia = date("d");
			$mes = date("m");
			$ano = date("y");
			$num = rand(0, 2000);
			$query = "SELECT id FROM personal WHERE id = '$this->id'";
			$result = $this->cnn->ejecutar($query);
			
		}
		$this->id = " $array_nombre[0]$array_apellido[0]$array_puesto[0]$dia$mes$ano-$num";
	}
	
	public function update_personal(){
			$id = $_POST['tid'];
			$nombre = $_POST['tnombre_'];
			$apellido = $_POST['tapellido'];
			$puesto = $_POST['tpuesto'];
			$tsueldo = $_POST['tsueldo'];
			$tipo = $_POST['ttipo_tiempo'];
			$tiempo = $_POST['ttiempo_horas'];
			$query = "UPDATE personal SET nombre = '$nombre', apellido = '$apellido' , puesto = '$puesto', sueldo = '$tsueldo', tipo_tiempo = '$tipo' , tiempo_horas = '$tiempo'
					WHERE id = $id ";
			$result = $this->cnn->ejecutar($query);
			if ($result == true){
				header('location:index.php?action=ver_personal');
			}
			else {
				header('location:index.php?action=add_persona');
			}
	}
	
	public function update_usuario(){
		$id = $_POST['tid'];
		$nombre = $_POST['tnombre_'];
		$apellido = $_POST['tapellido'];
		$query = "UPDATE usuarios SET nombre = '$nombre', pass = '$apellido' WHERE id = $id";
			$result = $this->cnn->ejecutar($query);
			if ($result == true){
				header('location:index.php?action=ver_usuarios');
			}
			else {
				header('location:index.php?action=add_usuarios');
			}
	}
	
	public function add_new_personal(){
		if (isset($_POST['tnombre_'])){
			$this->get_id();
			//echo $this->id;
			$nombre = $_POST['tnombre_'];
			$apellido = $_POST['tapellido'];
			$puesto = $_POST['tpuesto'];
			$tsueldo = $_POST['tsueldo'];
			$tipo = $_POST['ttipo_tiempo'];
			$tiempo = $_POST['ttiempo_horas'];
			$query = "INSERT INTO personal VALUE ('$this->id', '$nombre', '$apellido' , '$puesto', '$tsueldo', '$tipo' , '$tiempo', '1')";
			$result = $this->cnn->ejecutar($query);
			if ($result == true){
				header('location:index.php?action=ver_personal');
			}
			else {
				header('location:index.php?action=add_persona');
			}
		}
	}
	
	public function add_new_usuario(){
	if (isset($_POST['tnombre_'])){
			//$this->get_id();
			//echo $this->id;
			$nombre = $_POST['tnombre_'];
			$apellido = $_POST['tapellido'];
			
			$query = "INSERT INTO usuarios VALUE (null, '$nombre', '$apellido')";
			$result = $this->cnn->ejecutar($query);
			if ($result == true){
				header('location:index.php?action=ver_usuarios');
			}
			else {
				header('location:index.php?action=add_usuarios');
			}
		}	
	}
	
	public function ver_personal(){
		include 'views/view_personal.php';
		$personal = new personal();
	}

	public function inicio(){
		$inicio = new view_inicio();
		if ($this->text != ''){
			$inicio->text = $this->text;
		}
		else{
			$inicio->text = 'listo';
		}
			$inicio->ver();
	}
	
	public function login(){
		//validar user and pass
		$query = "SELECT * FROM usuarios WHERE nombre = '$this->user_name'";
		$result = $this->cnn->ejecutar($query);
		//var_dump($result);
		$r = mysql_fetch_object($result);
		//var_dump($r);
		if ($r != false){
			if ($this->user_name == $r->nombre && $this->user_pass == $r->pass){
				$this->entry = '1';
				header("location:index.php?entrar=$this->entry&user=$this->user_name");
			}else {
				header("location:index.php?action=no_user&user=$this->user_name");
				
			}
		}else{
			header("location:index.php?action=no_user&user=$this->user_name");
		}
	}

	public function no_user(){
		require_once 'views/no-user.php';
		$view = new no_user();
		$view->name = $_REQUEST['user'];
		$view->ver();
	}
	
	public function entrar_login(){
		$_SESSION['username'] = $this->user_name;
		
		header("location:index.php?action=entrar");
		//$login = new login();
		//$login->name=$this->user_name;
		//$login->login();
		//header("location:views/view-login.php");
	}
	
	public function entrar(){
		$login = new login();
		//$login->name=$_SESSION['username'];
		$login->login();
	}
	
	public function close_session(){
		session_destroy();
		header("location:index.php");	
	}

} /* end of class modelo */

?>